<?php

/**
 * JsonqueyController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class JsonqueryController extends Zend_Controller_Action {
	
	function init() {
		parent::init ();
		Zend_Loader::loadClass ( 'Zend_Db' );
		Zend_Loader::loadClass ( 'Zend_Db_Table' );
		Zend_Loader::loadClass ( 'Zend_Json' );
		Zend_Loader::loadClass ( 'TaregModel' );
		Zend_Loader::loadClass ( 'TadisModel' );
		Zend_Loader::loadClass ( 'TagruModel' );
		Zend_Loader::loadClass ( 'TauniModel' );
		Zend_Loader::loadClass ( 'TanivModel' );
		
	}
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$this->view->data = array ( );
		$this->renderScript ( "json_result.tpl" );
	}
	
	/**
	 * consulta json de regiones
	 */
	public function obtenerregionesAction() {
		$tareg = new TaregModel ( );
		$datos = array ( );
		$datos ['identifier'] = "cReg";
		$datos ['label'] = "zReg";
		$regiones = $tareg->fetchAll ()->toArray ();
		array_walk_recursive($regiones, "htmlspecialchars");
		$datos ['items'] = $regiones;
		 $this->_helper->json($datos);
		//$this->view->data = json_encode($datos);
		//$this->view->template_base = "json_result.tpl";
	}
	
	/**
	 * consulta json de distritos
	 */
	public function obtenerdistritosAction() {
		$tabla = new TadisModel ( );
		
		$cReg = $this->_request->getParam ( "cReg" );
		
		$where = NULL;
		if (! empty ( $cReg )) {
			$where = "cReg = $cReg";
		}
		
		$datos = array ( );
		$datos ['identifier'] = "cDis";
		$datos ['label'] = "zDis";
		$distritos = $tabla->fetchAll ( $where )->toArray ();
		array_walk_recursive($distritos, "htmlspecialchars");
		$datos ['items'] = $distritos;
		
		 $this->_helper->json($datos);
		
		//$this->view->data = json_encode($datos);
		//$this->view->template_base = "json_result.tpl";
	}
	
	/**
	 * consulta json de grupos
	 */
	public function obtenergruposAction() {
		$tabla = new TagruModel ( );
		
		$cDis = $this->_request->getParam ( "cDis" );
		
		$where = NULL;
		if (! empty ( $cDis )) {
			$where = "cDis = $cDis";
		}
		
		$datos = array ( );
		$datos ['identifier'] = "cGru";
		$datos ['label'] = "zGru";
		$grupos = $tabla->fetchAll ( $where )->toArray ();
		array_walk_recursive($grupos, "htmlspecialchars");
		$datos ['items'] = $grupos;
		
		//$this->view->data = json_encode($datos);
		//$this->view->template_base = "json_result.tpl";
		 $this->_helper->json($datos);
		
	}
	
	/**
	 * consulta json de unidades
	 */
	public function obtenerunidadesAction() {
		$tabla = new TauniModel ( );
		
		$cUni = $this->_request->getParam ( "cUni" );
		$cNiv = $this->_request->getParam ( "cNiv" );
		
		$where = NULL;
		if (! empty ( $cUni )) {
			$where = "cUni = $cUni";
		}
		if (! empty ( $cNiv )) {
			$where = "cNiv between 1 and $cNiv";
		}
		
		$datos = array ( );
		$datos ['identifier'] = "cUni";
		$datos ['label'] = "zUni";
		$unidades = $tabla->fetchAll ( $where )->toArray ();
		array_walk_recursive($unidades, "htmlspecialchars");
		$datos ['items'] = $unidades;
		//var_dump($unidades);
		 $this->_helper->json($datos);
		//$this->view->data = json_encode($datos);
		//$this->view->template_base = "json_result.tpl";
	}

	/**
	 * consulta json de nivel
	 */
	public function obtenernivelesAction() {
		$tabla = new TanivModel ( );
		
		$maxNivel = $this->getRequest()->getParam("maxNivel", 4);
		$where = "cNiv between 1 and $maxNivel";
		
		$datos = array ( );
		$datos ['identifier'] = "cNiv";
		$datos ['label'] = "zNiv";

		$niveles = $tabla->fetchAll ( $where )->toArray ();
		array_walk_recursive($niveles, "htmlspecialchars");
		$datos ['items'] = $niveles;
		
		 $this->_helper->json($datos);
		//$this->view->data = json_encode($datos);
		//$this->view->template_base = "json_result.tpl";
		//$this->renderScript ( "json_result.tpl" );
	}
	
	/**
	 * consulta json de unidades
	 */
	public function listajovenesAction() {
		// load configuration
		$config = Zend_Registry::getInstance()->get('config');
		// setup database
		$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
		
		$cUni = $this->_request->getParam ( "cUni" );
		$cGru = $this->_request->getParam ( "cGru" );
		$sql="SELECT Credencial, Nombre, `Ultimo_Registro` FROM SRRASV.tempMiembros WHERE Credencial <= 1999999 and cUni = $cUni and cGru = $cGru ";
		
		$datos = array ( );
		$datos ['identifier'] = "Credencial";
		$datos ['label'] = "Nombre";
		$datos ['sql'] = $sql;
		
		$datos ['nombreTabla'] = "lista"; 
		$datos ['items'][] = array("Credencial" => "Credencial", "Nombre" => "Nombre", "Ultimo_Registro" => "Ultimo Registro");
		$datos ['items'] = array_merge($datos ['items'], $db->fetchAll($sql));

		array_walk_recursive($datos ['items'], "htmlspecialchars");
		
		//$this->view->data = json_encode($datos);
		//$this->view->template_base = "json_result.tpl";
		 $this->_helper->json($datos);
	}

	/**
	 * consulta json de unidades
	 */
	public function listaadultosAction() {
		// load configuration
		$config = Zend_Registry::getInstance()->get('config');
		// setup database
		$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
		
		$cUni = $this->_request->getParam ( "cUni" );
		$cReg = $this->_request->getParam ( "cReg" );
		$cDis = $this->_request->getParam ( "cDis" );
		$cGru = $this->_request->getParam ( "cGru" );
		
		$where = "";
		
		switch ($cUni) {
			case 11:
				$where = "cReg = $cReg";
				break;
			case 10:
				$where = "cDis = $cDis";
				break;
			default:
				$where = "cGru = $cGru";
				break;
		}
		
		$sql="SELECT Credencial, Nombre, `Ultimo_Registro` FROM SRRASV.tempMiembros WHERE Credencial >= 2000000 and cUni = $cUni and $where";
		
		$datos = array ( );
		$datos ['identifier'] = "Credencial";
		$datos ['label'] = "Nombre";
		$datos ['sql'] = $sql;
		
		$datos ['nombreTabla'] = "lista"; 
		$datos ['items'][] = array("Credencial" => "Credencial", "Nombre" => "Nombre", "Ultimo_Registro" => "Ultimo Registro");
		$datos ['items'] = array_merge($datos ['items'], $db->fetchAll($sql));
		array_walk_recursive($datos ['items'], "htmlspecialchars");
		 $this->_helper->json($datos);
		//$this->view->data = json_encode($datos);
		//$this->view->template_base = "json_result.tpl";
	}
}
